/*
 * @Description:工具方法
 * @Version: 2.0
 * @Autor: kafei
 * @Date: 2021-11-18 18:06:08
 * @LastEditors: kafei
 * @LastEditTime: 2021-12-09 17:12:54
 */
// 生成随机数
const moment = require('moment')
function guid () {
  return 'xxxxx-xxxx'.replace(/[xy]/g, (c) => {
    const r = (Math.random() * 16) | 0
    const v = c === 'x' ? r : (r & 0x3) | 0x8
    return v.toString(16)
  })
}

// 格式化日期
function handleDate(date) {
  if (!date || date === undefined || date === null) {
    return ''
  }
  return moment(date).format('YYYY-MM-DD')
}

// 统一处理数据库日期方法
function momentResultDateFn(array) {
  const targetkey = ['createTime']
  if (!array || array.length === 0) {
    return []
  }
  return array.map(v => {
    for (const key in v) {
      if (targetkey.includes(key)) {
        v[key] = handleDate(v[key])
      }
    }
    return v
  })
}

// 过滤对象key
function filterObj(obj, keys) {
  // const keys = ['userName', 'userId', 'depId', 'createTime', 'passWord', 'rid', 'telphone', 'nickName', 'status', 'job', 'isLeave', 'remarks', 'id']
  const target = {}
  for (const key in obj) {
    if (keys.includes(key)) {
      target[key] = obj[key] || ''
    }
  }
  return target
}

module.exports = {
  guid,
  handleDate,
  momentResultDateFn,
  filterObj
}
